﻿//功能描述:系统管理-角色权限设置
using Dapper;
using Hong.Common.Encryption;
using Hong.IRepository.IRepository_SystemSettings.RBAC;
using Hong.Model.Dto.PesponseDto.ResponseModel.ResultModel;
using Hong.Model.SysDomain.Model_SystemSettings.RBAC;
using Hong.Model.SysDomain.Model_SystemSettings.ViewModel;
using Hong.Repository.Data;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hong.Repository.Repository.Repository_SystemSettings
{
    public class RoleinfoDal : IRole
    {
        #region 构造器注入

        private readonly DapperHelper _dapperHelper;
        private readonly SecurityStatic _securityStatic;


        public RoleinfoDal(SecurityStatic  securityStatic, DapperHelper dapperHelper)
        {
            _dapperHelper = dapperHelper;

            _securityStatic=securityStatic;
        }
        #endregion

        #region 添加角色
        public ResponseModel<bool> AddUR(string[] PersonnelId, string RoleNmae)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                ResponseModel<bool> res = new ResponseModel<bool>();
                try
                {
                    //全球唯一标识符
                    var RoleId = Guid.NewGuid();
                    conn.Open();
                    using (var begin = conn.BeginTransaction())
                    {
                        //角色查重
                        var q = conn.Query($"select * from Roleinfo where RoleNmae='{RoleNmae}'", null, begin).ToList();
                        if (q.Count != 0)
                        {
                            return res.Succeed(false);
                        }
                        else
                        {
                            //添加角色
                            conn.Execute($"insert into Roleinfo values('{RoleId}','{RoleNmae}')", null, begin);
                            if (PersonnelId.Length != 0)
                            {

                                foreach (var item in PersonnelId)
                                {

                                    //添加角色人员
                                    conn.Execute($"insert into  User_Role values('{item}','{RoleId}')", null, begin);
                                }

                            }
                            begin.Commit();

                            return res.Succeed(true);
                        }

                    }

                }
                catch (Exception ex)
                {

                    throw;
                }



            }
        }

        #endregion


        #region 加载角色
        public ResponseModel<List<RoleModel>> GetRole()
        {
            ResponseModel<List<RoleModel>> res = new ResponseModel<List<RoleModel>>();
            var sql = $"select * from show_User_Role";
            var list = _dapperHelper.Query<RoleModel>(sql).ToList();

            return res.Succeed(list);
        }

        #endregion


        #region 获取全部用户
        public ResponseModel<List<GetPersonModel>> GetPerson()
        {
            ResponseModel<List<GetPersonModel>> res = new ResponseModel<List<GetPersonModel>>();
            var sql = $"select p.PersonnelId,  p.PersonnelName,d.depName from Personnel p join Department d on p.DepartmentId=d.depId where p.JobStatus='在职' order by d.depName";

            var list = _dapperHelper.Query<GetPersonModel>(sql).ToList();

            return res.Succeed(list);
        }
        #endregion

        #region 反填
        public ResponseModel<List<RoleModel>> GURole(string RoleId)
        {
            ResponseModel<List<RoleModel>> res = new ResponseModel<List<RoleModel>>();
            var sql = $"select PersonnelId from User_Role where RoleId='{RoleId}' group by PersonnelId ";
            var list = _dapperHelper.Query<RoleModel>(sql).ToList();

            return res.Succeed(list);
        }

        #endregion

        #region 修改角色
        public ResponseModel<bool> UPRole(string[] PersonnelId, string RoleNmae, string RoleId)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                ResponseModel<bool> res = new ResponseModel<bool>();
                try
                {
                    conn.Open();
                    using (var begin = conn.BeginTransaction())
                    {
                        //修改角色表
                        conn.Execute($"update  Roleinfo set RoleNmae='{RoleNmae}' where RoleId='{RoleId}'", null, begin);
                        
                            //判断是有绑定用户,删除
                            conn.Execute($"delete from User_Role where RoleId='{RoleId}'", null, begin);
                        
                        
                            foreach (var item in PersonnelId)
                            {

                                //未绑定用户直接添加角色人员
                                conn.Execute($"insert into  User_Role values('{item}','{RoleId}')", null, begin);
                            }
                        
                        begin.Commit();

                        return res.Succeed(true);
                    }

                }
                catch (Exception)
                {

                    throw;
                }


            }
        }
        #endregion

        #region 删除角色
        public ResponseModel<bool> DelRole(string RoleId)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                ResponseModel<bool> res = new ResponseModel<bool>();
                try
                {
                    conn.Open();
                    using (var begin = conn.BeginTransaction())
                    {
                        conn.Execute($"delete from Roleinfo where RoleId='{RoleId}'", null, begin);
                        conn.Execute($"delete from User_Role where RoleId='{RoleId}'", null, begin);
                        begin.Commit();
                        return res.Succeed(true);
                    }

                }
                catch (Exception)
                {

                    throw;
                }
            }

        }
        #endregion


    }
}
